/**
* @program: src
*
* @description: 路由文件
*
* @author: Mr.chen
*
* @create: 2022-04-02 15:30
**/
package handler

import (
	"do_demo/srv/app/logic/api/internal/config"
	"do_demo/srv/app/logic/api/internal/handler/middleware/prometheus"
	"do_demo/srv/app/logic/api/internal/handler/middleware/tracing"
	"do_demo/srv/app/logic/api/internal/logic"
	"do_demo/srv/app/logic/api/internal/svc"
	"do_demo/srv/common/core/logx"
	"github.com/gin-gonic/gin"
	"net/http"
)

// Server is http server.
type Server struct {
	engine *gin.Engine
	logic *logic.Logic
}

// New new a http server.
func New(svc *svc.ServiceContext) *Server {
	c := config.Service(svc.Config)
	if err := c.SetUp(); err != nil {
		panic(err)
	}
	// 关闭调试模式的日记
	gin.SetMode(gin.ReleaseMode)
	gin.DisableBindValidation()
	//gin.DisableConsoleColor()
	s := &Server{
		engine: gin.New(),
		logic: logic.NewLogic(svc),
	}
	s.engine.Use(prometheus.PrometheusHandler(),recoverHandler,tracing.TracingHandler())
	// 路由与逻辑绑定
	s.RegisterHandlers()
	srv := &http.Server{
		Addr:    svc.Config.HTTPServer.Addr,
		Handler: s.engine,
	}
	logx.Infof("start http agent at  0.0.0.0%s",svc.Config.HTTPServer.Addr)

	go func() {
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			panic(err)
		}
	}()
	return s
}
// Close close the server.
func (s *Server) Close() {
}
